package org.cn.osiris.system.service.impl;

import org.cn.osiris.org.domain.Department;
import org.cn.osiris.org.mapper.DepartmentMapper;
import org.cn.osiris.system.domain.Menu;
import org.cn.osiris.system.mapper.MenuMapper;
import org.cn.osiris.system.service.IMenuService;
import org.cn.osiris.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements IMenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Menu> menuTree() {
        List<Menu> allMenus = menuMapper.findAll();
        HashMap<Long,Menu> allMenuMap = new HashMap<>();
        List<Menu> menuTree = new ArrayList<>();
        for (Menu dept : allMenus){
            allMenuMap.put(dept.getId(),dept);
        }
        for (Menu temp :allMenus){
            if(temp.getParentId()==null){
                menuTree.add(temp);
            }else{
                Menu parent = allMenuMap.get(temp.getParentId());
                if(parent!=null){
                    parent.getChildren().add(temp);
                }
            }
        }
        System.out.println(menuTree);
        return menuTree;
    }
}
